//
//  HomeNewsDetailViewController.m
//  yunyan
//
//  Created by haichuang on 2018/3/27.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "HomeNewsDetailViewController.h"
#import "HomeNewsDetailCell.h"
#import "CommentCell.h"

#import "InputView.h"
#import "ShareView.h"
@interface HomeNewsDetailViewController ()<UITableViewDelegate, UITableViewDataSource,XHInputViewDelagete>
@property (nonatomic ,strong) UIImageView * imageView;
@property (nonatomic, strong) UITableView * activeDetailTab;
@property (nonatomic, strong)   UIView * GRbackgView;
@property (nonatomic, strong) ShareView *shareView;
@end

@implementation HomeNewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self customNaviItemTitle:@"新闻详情" isLeft:YES];
    [self customTabBarButtonTitle:nil image:[UIImage imageNamed:@"new_share_default"] target:self action:@selector(shareBtn) isLeft:NO];
    
    [self createDetailUI];
}
-(void)createDetailUI{
    
    WeakSelf;
    _activeDetailTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:_activeDetailTab ];
    [_activeDetailTab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo (weakSelf.view);
        make.bottom.equalTo (weakSelf.view.mas_bottom).offset (-50.0);
        
    }];
    
    _activeDetailTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _activeDetailTab.backgroundColor = RGBA(243, 245, 248, 1);
    self.activeDetailTab.estimatedRowHeight = 44.f;
    self.activeDetailTab.rowHeight = UITableViewAutomaticDimension;
    _activeDetailTab.delegate = self;
    _activeDetailTab.dataSource = self;
    [_activeDetailTab registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
    [_activeDetailTab registerClass:[HomeNewsDetailCell class] forCellReuseIdentifier:@"HomeNewsDetailCell"];

    
    if ( _isDetail) {
        self.activeDetailTab.backgroundColor = RGBA(242, 247, 247, 1);
    }
    
    UIView * buttomView = [[UIView alloc] init];
    [buttomView setViewRoundShadow];
    buttomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:buttomView];
    [buttomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo (weakSelf.view);
        make.height.equalTo (@(50.f));
    }];
    UITapGestureRecognizer * neargrGes=[[UITapGestureRecognizer  alloc]initWithTarget:self action:@selector(bottomGes)];
    [buttomView addGestureRecognizer:neargrGes];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [buttomView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo (buttomView).offset (18.0f);
        make.width.height.equalTo (@(14.0f));
        
    }];
    imageView.image = [UIImage imageNamed:@"new_input_default"];
    UILabel * label = [[UILabel alloc]init];
    [buttomView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo (imageView.mas_right).offset (6.0f);
        make.top.bottom.equalTo (imageView);
        
    }];
    label.text = @"畅言一下...";
    label.font = [UIFont boldSystemFontOfSize:12.0];
    label.textColor = RGBA(51, 51, 51, 1);
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)bottomGes{
    [self showXHInputViewWithStyle:InputViewStyleLarge];
}


#pragma mark- share
-(void)shareBtn{
    
    [self CreateShareViewAppear];
    
}

-(void)CreateShareViewAppear{
    
    WeakSelf;
    self.navigationItem.rightBarButtonItem.enabled = NO;
    _GRbackgView = [[UIView alloc] init];
    
    [self.view addSubview:_GRbackgView];
    [_GRbackgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(weakSelf.view);
        
    }];
    
    _GRbackgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.10];
    [self.view addSubview:_GRbackgView];
    
    _shareView = [[ShareView alloc]init];
    
    [self.view addSubview:_shareView];
    
    
    [_shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@200);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
    _shareView.backgroundColor = [UIColor whiteColor];
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = _shareView.frame;
        frame.origin.y = -200;
        
        [_shareView setFrame:frame];
        
        
    } completion:^(BOOL finished) {
        
    }];
    _shareView.cancelBlock = ^{
        [weakSelf geGes];
    };
    
    UITapGestureRecognizer * neargrGes=[[UITapGestureRecognizer  alloc]initWithTarget:self action:@selector(geGes)];
    [_GRbackgView addGestureRecognizer:neargrGes];
    
}



-(void)geGes{
    
    
    [UIView animateWithDuration:0.8 animations:^{
        
        
        _shareView.alpha = 0.0;
        _GRbackgView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        
        [_shareView removeFromSuperview];
        [_GRbackgView removeFromSuperview];
        
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }];
    
}

#pragma mark - tableview delegete;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section  == 1) {
        return 10;
    }
    else{
        return 1;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return  50;
    }else{
        
        return CGFLOAT_MIN;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        
        UIView * view = [[UIView alloc]init];
        UIImageView * image = [[UIImageView alloc] init];
        [view addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo (view.mas_left).offset (100.0f);
            make.right.equalTo (view.mas_right).offset (-100.0f);
            make.top.equalTo (view.mas_top).offset (10.0f);
            make.bottom.equalTo (view.mas_bottom).offset (-10.0f);
            
        }];
        image.image = [UIImage imageNamed:@"new_title_pressed"];
        
        return view;
    }else {
        
        return nil;
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
    
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        HomeNewsDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeNewsDetailCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        
    }else {
        
        
        CommentCell * cell=[tableView dequeueReusableCellWithIdentifier:@"CommentCell"forIndexPath:indexPath ];
        cell.backgroundColor = RGBA(243, 245, 248, 1);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
}

-(void)showXHInputViewWithStyle:(InputViewStyle)style{
    
    [InputView showWithStyle:style configurationBlock:^(InputView *inputView) {
        /** 请在此block中设置inputView属性 */
        
        /** 代理 */
        inputView.delegate = self;
        
        /** 占位符文字 */
        // inputView.placeholder = @"请输入评论文字噢噢噢噢";
        /** 设置最大输入字数 */
        inputView.maxCount = 50;
        /** 输入框颜色 */
        inputView.textViewBackgroundColor = [UIColor groupTableViewBackgroundColor];
        
        /** 更多属性设置,详见XHInputView.h文件 */
        
    } sendBlock:^BOOL(NSString *text) {
        if(text.length){
            NSLog(@"输入的信息为:%@",text);
            
            return YES;//return YES,收起键盘
        }else{
            NSLog(@"显示提示框-请输入要评论的的内容");
            return NO;//return NO,不收键盘
        }
        
    }];
    
}
#pragma mark - XHInputViewDelagete
/** XHInputView 将要显示 */
-(void)xhInputViewWillShow:(InputView *)inputView{
    
    /** 如果你工程中有配置IQKeyboardManager,并对XHInputView造成影响,请在InputView将要显示时将其关闭 */
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].enable = NO;
    
}

/** XHInputView 将要影藏 */
-(void)xhInputViewWillHide:(InputView *)inputView{
    
    /** 如果你工程中有配置IQKeyboardManager,并对XHInputView造成影响,请在XHInputView将要影藏时将其打开 */
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [IQKeyboardManager sharedManager].enable = YES;
}
@end
